package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.UserZhenHuanSecondProcessData;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.UserZhenHuanSecondRelationData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserZhenHuanSecondRelationDaoImpl extends TempDataObjectDao<UserZhenHuanSecondRelationData> {
    public static String getUserZhenHuanSecondRelationDataSql = "select * from `t_u_zhenhuan_second_relation` where `activityId`=? and `userId`=?";
    public Map<Long, UserZhenHuanSecondRelationData> getUserZhenHuanSecondRelationData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UserZhenHuanSecondRelationData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserZhenHuanSecondRelationDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserZhenHuanSecondRelationData data = parse(rs);
                dataMap.put(data.getTargetUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUserZhenHuanSecondRelationDataSql = "update `t_u_zhenhuan_second_relation` set joinAmount=?,finishAmount=? where activityId=? and userId=? and targetUserId=?";
    @Override
    protected boolean updateData(UserZhenHuanSecondRelationData userZhenHuanSecondRelationData) {
        userZhenHuanSecondRelationData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanSecondRelationData.getJoinAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanSecondRelationData.getFinishAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanSecondRelationData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanSecondRelationData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanSecondRelationData.getTargetUserId()));
        if (getDb().executeNoneQuery(updateUserZhenHuanSecondRelationDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserZhenHuanSecondRelationDataSql = "insert into `t_u_zhenhuan_second_relation`(activityId,userId,targetUserId,joinAmount,finishAmount) values (?,?,?,?,?)";
    @Override
    protected boolean addData(UserZhenHuanSecondRelationData userZhenHuanSecondRelationData) {
        userZhenHuanSecondRelationData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanSecondRelationData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanSecondRelationData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanSecondRelationData.getTargetUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanSecondRelationData.getJoinAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanSecondRelationData.getFinishAmount()));
        if (getDb().executeNoneQuery(insertUserZhenHuanSecondRelationDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserZhenHuanSecondRelationData parseData(ResultSet rs) throws SQLException {
        UserZhenHuanSecondRelationData userZhenHuanSecondRelationData = new UserZhenHuanSecondRelationData();
        userZhenHuanSecondRelationData.setActivityId(rs.getInt("activityId"));
        userZhenHuanSecondRelationData.setUserId(rs.getLong("userId"));
        userZhenHuanSecondRelationData.setTargetUserId(rs.getLong("targetUserId"));
        userZhenHuanSecondRelationData.setJoinAmount(rs.getInt("joinAmount"));
        userZhenHuanSecondRelationData.setFinishAmount(rs.getInt("finishAmount"));

        return userZhenHuanSecondRelationData;
    }
}
